import Search from 'models/search';

import moment from 'moment';

export default class OrderRecordSearch extends Search {
  branchId = '';
  partnerId = '';
  orderNoMain = '';
  buyerName = '';
  buyerPhone = '';
  status = '';
  startTime = null;
  endTime = null;

  propertyList = ['branchId', 'partnerId', 'orderNoMain', 'buyerName', 'buyerPhone', 'status', 'startTime', 'endTime'];

  constructor() {
    super();
    this.setOldData();
  }

  checkValid() {
    let startTime = this.startTime;
    let endTime = this.endTime;

    if (startTime !== null && endTime !== null) {
      if (moment(startTime).isAfter(endTime, 'second')) {
        return 'startTime>endTime';
      }
    }

    return '';
  }

  getObject(pagination) {
    let postData = {};

    let fields = ['branchId', 'partnerId', 'orderNoMain', 'buyerName', 'buyerPhone', 'status'];
    this.setPostData(postData, fields);

    let startTime = this.startTime;
    if (startTime !== null) {
      postData.createTimeStart = moment(startTime).format('YYYY-MM-DD HH:mm:ss');
    }

    let endTime = this.endTime;
    if (endTime !== null) {
      postData.createTimeEnd = moment(endTime).format('YYYY-MM-DD HH:mm:ss');
    }

    if (pagination !== undefined) {
      let {
        limit,
        page
      } = pagination.getObject();
      postData.limit = limit;
      postData.page = page;
    }

    return postData;
  }
}
